package com.gaswell.aop.aspect;

import com.gaswell.aop.annotation.JHPermission;
import com.gaswell.pojo.ALLQj;
import com.gaswell.service.ALLQjService;
import com.gaswell.service.UserService;
import com.gaswell.utils.HttpContextUtils;
import com.gaswell.utils.JwtUtils;
import com.gaswell.vo.Result;
import io.jsonwebtoken.Claims;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Aspect
@Component
@Order(2)
public class JHPermAspect {

    @Autowired
    ALLQjService allQjService;


    @Pointcut("@annotation(com.gaswell.aop.annotation.JHPermission)")
    public void jhPointCut() {

    }

    @Around(value = "jhPointCut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        int zwbh_index = -1;
        int ywbh_index = -1;
        int jh_index = -1;
        final Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;

        JHPermission annotation = methodSignature.getMethod().getAnnotation(JHPermission.class);

        final String[] names = methodSignature.getParameterNames();//获取参数的名字
        final Object[] args = joinPoint.getArgs();// 获取参数的值
//   获取token，中文井号列表和英文井号列表的参数位置
        for (int i = 0; i < args.length; i++) {
            if (names[i].equals("ywbhs")) {
                ywbh_index = i;
            }

            if (names[i].equals("zwbhs")) {
                zwbh_index = i;
            }

            if (names[i].equals("jh")) {
                jh_index = i;
            }
        }


        String token = HttpContextUtils.getHeadersValue("token");
        Claims claims = JwtUtils.getClaims(token);
        String depart_id = claims.get("departid", String.class);
        String depart_name = "";
        if (depart_id == "1")
            depart_name = null;
        else if (depart_id.equals("2"))
            depart_name = "川渝";
        else
            depart_name = "大庆";


//      判断查询数字化建设还是全部井号
        List<String> ywjhs;
        List<String> zwjhs;
        if (annotation.sfszhjs() == -1) {
            ywjhs = allQjService.findYwjh(depart_name);
            zwjhs = allQjService.findZwjh(depart_name);
        } else {
            ywjhs = allQjService.findYwjh(depart_name, annotation.sfszhjs());
            zwjhs = allQjService.findZwjh(depart_name, annotation.sfszhjs());
        }

        if (jh_index == -1 || args[jh_index].equals(" ")) {
            args[zwbh_index] = zwjhs;
            args[ywbh_index] = ywjhs;
        } else {
            int zyw_jh_index = -1;
            for (int i = 0; i < zwjhs.size(); i++) {
                if (zwjhs.get(i).equals(args[jh_index]))
                    zyw_jh_index = i;
                if (ywjhs.get(i).equals(args[jh_index]))
                    zyw_jh_index = i;
            }

//        没有权限查询
            if (zyw_jh_index == -1)
                return Result.fail(403, "没有权限查询");

//          有权限查询，就查他自己就可以了
            String zwjh = zwjhs.get(zyw_jh_index);
            String ywjh = ywjhs.get(zyw_jh_index);
            zwjhs.clear();
            ywjhs.clear();
            zwjhs.add(zwjh);
            ywjhs.add(ywjh);

            args[zwbh_index] = zwjhs;
            args[ywbh_index] = ywjhs;
        }
        return joinPoint.proceed(args);
//        return null;


    }


}